home *** CD-ROM | disk | FTP | other *** search
- /*
- * Listing 3 -- Error by writing a freed pointer
- * (another item's value may have changed)
- */
-
- #include <stdlib.h>
- #include <assert.h>
-
- void main() {
- int *ip1;
- int *ip2;
-
- /* allocate an integer */
- ip1 = malloc(sizeof(int));
- assert(ip1 != NULL);
- (*ip1) = 0;
-
- /* deallocate the integer */
- free(ip1);
-
- /* allocate a second integer */
- ip2 = malloc(sizeof(int));
- assert(ip2 != NULL);
- (*ip2) = 0;
- /* note that ip1 is probably equal to ip2 */
- printf("ip1 == 0x%lx, ip2 == 0x%lx\n",ip1,ip2);
-
- (*ip1) = 5;
- printf("*ip2 == %d\n",*ip2);
- }
-